import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import FormModal from '#cpt/temp/FormModal'
import TableControls from '#cpt/temp/TableControls'
// ===================================================================== global declare
const { $fn, $http, $Lazy } = window
// ===================================================================== $Lazy
const $cache 		= $Lazy.file(()=>import('#private/cache-api'))
const $time 		= $Lazy.file(()=>import('@com/utils/time'))
const ChangeSpecModal 	= $Lazy.state(()=>import('./change-spec-modal'))
// ===================================================================== data
const sourceData = [{name:'在线商城',value:'1'},{name:'物流小程序',value:'2'},{name:'采样小程序',value:'3'},{name:'未知来源',value:'0'}]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys: [],
		selectedRows: [],
		// 搜索表单列表
		forms : [
			{ label: '条码号',		name: 'spec_code' },
			{ label: '项目名称',		name: 'kind_code',		type:'select',	loading:true },
			{ label: '姓名',			name: 'check_name' },
			{ label: '创建日期',		names: ['start_at','end_at'], 	type:'date-range', 	loading:true,	name:'jz-time' },
			{ label: '手机号',		name: 'check_phone' },
			{ label: '证件号',		name: 'card_no' },
			// 添加来源  2022-5-24  --马婷
			{ label: '来源',			name: 'source',		type:'select',	data:sourceData },
		],
		// 导入Lis系统
		formData:[
			{ label: '条码号', 		name:'spec_codes', 	full:true,	type:'textarea',	required:true },
			{ label: '标本箱号',		name: 'box_code',	full:true,	type: 'select',		required:true,	loading:true,	idStr:'box_code', nameStr:'box_code'},
		],
	}
	componentDidMount(){
		this.getSelect()
		
		$time.then(f=>{				 /* 添加默认创建时间 2022-5-24 --马婷 */
			const arr = f.duration['30']()
			this.resetModel = { start_at: arr[0], end_at: arr[1], 'jz-time':arr }
			this.model = $fn.copy(this.resetModel)
			if(window.isInit){
				this.fetch()
			}
		})
		
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 项目名称
			$fn.setRow( forms, 'kind_code', await f.getCache({ api:'combinKindSelect' }) )
			
			this.setState({ forms })
		})
	}
	// 弹窗获取下拉数据
	getModalSelect = () => {
		const { formData } = this.state
		$cache.then( async f => {
			//标本箱号
			$fn.setRow( formData, 'box_code', await f.getCache({ api:'box/select',param:{status:81,limit:1000},way:'post',refresh:true}) )
			
			this.setState({ formData })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'internet-gather/index', {isIndex:true,param:{is_delete:'1'}})
	
	// 表单
	cols = [
		{ title: '条码号', 		dataIndex: 'spec_code', 		...$fn.table.spec_code },
		{ title: '项目名称', 	dataIndex: 'kind_name', 		width:180 },
		{ title: '姓名', 		dataIndex: 'check_name', 		...$fn.table.name },
		{ title: '年龄', 		dataIndex: 'age', 				width:60 },
		{ title: '性别', 		dataIndex: 'sex', 				width:60,		render: text => {
			switch (+text){
				case 45701:
					return '男';
				case 45702:
					return '女';
				default:
					return '/';
			}
		} },
		{ title: '医院', 		dataIndex: 'hosp_name', 	width:180 },
		{ title: '来源', 		dataIndex: 'source_name', 	width:120 },
		{ title: '手机号', 		dataIndex: 'check_phone', 	...$fn.table.phone },
		{ title: '证件类型', 	dataIndex: 'card_type_name', ...$fn.table.card_type },
		{ title: '证件号', 		dataIndex: 'card_no', 		...$fn.table.idcard },
		{ title: '创建时间', 	dataIndex: 'created_at',  	...$fn.table.time},
		TableControls.controls.call(this,{ width: 100 })
	]
	
	// 操作按钮
	controls = () => {
		const {selectedRows} = this.state
		let spec_codes = selectedRows.map(v => v.spec_code)
		return [
			Controls.click.call(this, '导入Lis系统', () => {
				this.getModalSelect()
				if(spec_codes){
					spec_codes = Array.from(new Set(spec_codes))
					this.formRef.setValue({spec_codes:spec_codes.join('\r') })
				}
				this.modal.open({formRef: this.formRef})
			}, { isKeys:1} ),
		]
	}	
	
	// 表格操作按钮
	table_controls = ( text, row ) => {
		return [
			TableControls.click.call(this,'修改条码',()=> this.changeSpecModal.open(row) )
		]
	}
	
	render(){
		return (
		<>
			{/* 搜索 */}
			{ SearchForm.call(this, {timeStr:'30'}) }
			{/* 表格 */}
			{ Table.call(this, {isIndex:true, isInit: window.isInit}) }
			{/* 修改条码 */}
			<ChangeSpecModal onRef = { ref => this.changeSpecModal = ref } onRefresh={()=>this.fetch()} />
			{/* 导入Lis系统 */}
			{FormModal.call(this,{ title:'导入Lis系统', labelWidth:'s5', width:800, api:'',enter:false,
				onSubmit: ()=> {
					let {spec_codes, box_code} = this.formRef.getValue()
					if(spec_codes){
						spec_codes = spec_codes.match(/\d{14}|\d{12}/g)
						spec_codes = Array.from(new Set(spec_codes))
					}
					$http.submit(null,'internet-gather/import',{ param:{spec_codes,box_code}}).then(data=>{
						if(data.length){
							const errCode = data.join(',')
							this.modal.close()
							$fn.msg.warning(`条码【${errCode}】有问题，请另行处理`)
						}else{
							$fn.msg.success('操作成功')
							this.modal.close()
						}
						this.fetch()
						this.setState({selectedKeys:[],selectedRows:[]})
					})
				}
			})}
		</>
			
		)
	}
}

export default Index
 /* 标本管理添加云采集页面 2021-8-10 --马婷 */